package com.aqie.arithmetic.draw;

import com.aqie.libs.*;

import java.util.Arrays;

public class Draw {
    public static void main(String[] args) {
        // drawCurve();
        // drawRandomArray();
        // drawSortedArray();
        drawInterval2D(.2,.5,.5,.6, 10000);
    }

    // 绘制函数
    public static void drawCurve(){
        int N = 100;
        StdDraw.setXscale(0, N);
        StdDraw.setYscale(0, N * N);
        StdDraw.setPenRadius(.01);
        for (int i = 1; i <= N; i++){
            StdDraw.point(i, i);
            StdDraw.point(i, i * i);
            StdDraw.point(i, i * Math.log(i));
        }
    }

    // 绘制随机数组
    public static void drawRandomArray(){
        int N = 50;
        double[] a = new double[N];
        for (int i = 0; i < N; i++){
            a[i] = StdRandom.uniform();
        }
        drawHistogram(N, a);
    }

    // 绘制排序数组
    public static void drawSortedArray(){
        int N = 50;
        double[] a = new double[N];
        for (int i = 0; i < N; i++){
            a[i] = StdRandom.uniform();
        }
        Arrays.sort(a);
        drawHistogram(N, a);
    }

    private static void drawHistogram(int n, double[] a) {
        for (int i = 0; i < n; i++){
            double x = 1.0 * i / n;
            double y = a[i] / 2.0;
            double rw = 0.5 / n;
            double rh = a[i] / 2.0;
            StdDraw.filledRectangle(x, y, rw, rh);

        }
    }

    // 绘制二维矩形, 查看点命中
    private static void drawInterval2D(double xlo, double xhi, double ylo, double yhi,int T){
        Interval1D xi = new Interval1D(xlo, xhi);
        Interval1D yi = new Interval1D(ylo, yhi);
        Interval2D box = new Interval2D(xi, yi);
        box.draw();

        Counter c = new Counter("hits");
        for (int t = 0; t < T; t++){
            double x = Math.random();
            double y = Math.random();
            Point2D p = new Point2D(x, y);
            if (box.contains(p)){
                c.increment();
            }else {
                p.draw();
            }
        }
        StdOut.println(c);
        StdOut.println(box.area());
    }
}
